package com.zhou.util;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;

/**
 * @author lang.zhou
 * @since 2023/2/5
 */
public class ListUtil {

    public static <T> T getFirst(Collection<T> list){
        if(list != null && list.size() > 0){
            if(list instanceof List){
                return ((List<T>) list).get(0);
            }else{
                for (T t : list) {
                    return t;
                }
            }
        }
        return null;
    }
    public static <T> T getFirstOr(Collection<T> list,T t){
        T t1 = getFirst(list);
        return t1 == null ? t : t1;
    }


    public static <T> List<List<T>> partition(List<T> list, int size) {
        if(list != null){
            return Lists.partition(list,size);
        }
        return null;
    }

    /**
     * 对象升序排序
     */
    public static <T,K extends Comparable<K>> void sortLow2High(List<T> list, Function<T,K> func) {
        if(list != null && list.size() > 1){
            list.sort(low2HighObjectCompare(func));
        }
    }
    /**
     * 对象降序排序
     */
    public static <T,K extends Comparable<K>> void sortHigh2Low(List<T> list, Function<T,K> func) {
        if(list != null && list.size() > 1){
            list.sort(highLow2ObjectCompare(func));
        }
    }

    /**
     * 升序排序
     */
    public static <T extends Comparable<T>> void sortLow2High(List<T> list) {
        if(list != null && list.size() > 1){
            list.sort(low2HighCompare());
        }
    }

    public static <T,K extends Comparable<K>> Comparator<T> low2HighObjectCompare(Function<T,K> func) {
        return (o1, o2) -> {
            K c1 = func.apply(o1);
            K c2 = func.apply(o2);
            if(c1 == null && c2 == null){
                return 0;
            }else if(c1 == null){
                return -1;
            }else if(c2 == null){
                return 1;
            }else{
                return c1.compareTo(c2);
            }
        };
    }
    public static <T,K extends Comparable<K>> Comparator<T> highLow2ObjectCompare(Function<T,K> func) {
        return (o1, o2) -> {
            K c1 = func.apply(o1);
            K c2 = func.apply(o2);
            if(c1 == null && c2 == null){
                return 0;
            }else if(c1 == null){
                return 1;
            }else if(c2 == null){
                return -1;
            }else{
                return c2.compareTo(c1);
            }
        };
    }
    public static <T extends Comparable<T>> Comparator<T> low2HighCompare() {
        return (o1, o2) -> {
            if(o1 == null && o2 == null){
                return 0;
            }else if(o1 == null){
                return -1;
            }else if(o2 == null){
                return 1;
            }else{
                return o1.compareTo(o2);
            }
        };
    }

    public static <T extends Comparable<T>> Comparator<T> high2LowCompare() {
        return (o1, o2) -> {
            if(o1 == null && o2 == null){
                return 0;
            }else if(o1 == null){
                return 1;
            }else if(o2 == null){
                return -1;
            }else{
                return o2.compareTo(o1);
            }
        };
    }

    /**
     * 降序排序
     */
    public static <T extends Comparable<T>> void sortHigh2Low(List<T> list) {
        if(list != null && list.size() > 1){
            list.sort(high2LowCompare());
        }
    }

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(3);
        list.add(2);
        list.add(1);
        list.add(null);
        ListUtil.sortHigh2Low(list);
        System.out.println();
    }
}
